﻿using UnityEngine;
using System.Collections.Generic;
using ProtoBuf.Message;
using Dict.DataModel;

namespace Room
{
    public class RCAIChariotFactory : RCAIUnionFightBase
    {
        #region 数据
        public InstUFCariotFactory instData;
        public TableUnionFightFactory dictData;
        #endregion
        
        
        public RCAIChariotFactory(RoomCharactor owner) : base(owner)
        {
            this.type = AIType.UnionChariotFactory;
        }

        public void Init(InstUFCariotFactory instData)
        {
            UpdateData(instData);
            InitFSM();
        }

        public void UpdateData(InstUFCariotFactory instData)
        {
            this.instData = instData;
            this.dictData = Dict.Blo.DictUnionFightFactoryBlo.GetUnionFightFactoryByID(instData.factoryId + "");
            this.camp = dictData.Camp;
            var url = dictData.ModelUrl;
            FindSceneObj(url);
            if(objAni!=null)
            {
                objAni.playAutomatically = false;
            }
        }
        


        #region FSM
        private UnionChariotFactoryState currState = UnionChariotFactoryState.None;
        public UnionChariotFactoryState CurrState
        {
            get
            {
                return currState;
            }
        }

        public RCAIChariotFactoryBase currentState;

        private Dictionary<UnionChariotFactoryState,RCAIChariotFactoryBase> allState = new Dictionary<UnionChariotFactoryState, RCAIChariotFactoryBase>();
        public override void Tick()
        {
            base.Tick();
            //if (currState != UnionChariotFactoryState.None)
            //    allState[currState].Tick();
            if (currentState != null)
                currentState.Tick();
        }

        private void InitFSM()
        {
            allState[UnionChariotFactoryState.None] = new RCAIChariotFactoryBase();
            allState[UnionChariotFactoryState.Actived] = new RCAIChariotFactoryActived();
            allState[UnionChariotFactoryState.Unactived] = new RCAIChariotFactoryUnactived();
            allState[UnionChariotFactoryState.PrepareComplete] = new RCAIChariotFactoryPrepareComplete();
            allState[UnionChariotFactoryState.Complete] = new RCAIChariotFactoryComplete();
            foreach (var state in allState)
            {
                state.Value.controller = this;
            }
            SwitchState((int)UnionChariotFactoryState.None);
        }

        public override void SwitchState(int newState)
        {
            if ((int)currState != newState)
            {
                allState[currState].Leave();
                currState = (UnionChariotFactoryState)newState;
                allState[currState].Enter();
                currentState = allState[currState];
            }
        }
        #endregion


        public void PlayMakeCompleteEffect()
        {
            Spawn(RoomUnionFight.CHARIOT_FACTORY_COMPLETE, 1);
        }
    }
}